﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneShoppingDAL;
using OneShoppingModel;
using System.Linq.Expressions;
namespace OneShoppingBLL
{
    public class MenuBLL
    {
        private static MenuBLL instance;
        public static MenuBLL GetInstance()
        {
            if (instance == null)
            {
                instance = new MenuBLL();
            }
            return instance;
        }
        /// <summary>
        /// 根据Lambda表达式predicate条件，获取 Menu 单一实体
        /// </summary>
        /// <param name="predicate">获取实体的条件</param>
        /// <returns> Menu单一实体</returns>
        public Menu GetEntity(Expression<Func<Menu, bool>> predicate)
        {
            if (predicate != null)
                return LinqUtility.GetInstance().GetEntity<Menu>
                    (new OneShoppingDataContext(),
                predicate);
            else return null;

        }
        /// <summary>
        /// 获取Menu表的所有实体
        /// </summary>
        /// <returns>Menu集合</returns>
        public List<Menu> GetList()
        {
            return LinqUtility.GetInstance().GetList<Menu>
                    (new OneShoppingDataContext());
        }

        /// <summary>
        /// 根据Lambda表达式predicate条件，获取Menu集合
        /// </summary>
        /// <param name="predicate">获取列表的条件</param>
        /// <returns>Menu集合</returns>
        public List<Menu> GetList(Expression<Func<Menu, bool>> predicate)
        {
            if (predicate != null)
                return LinqUtility.GetInstance().GetList<Menu>
                    (new OneShoppingDataContext(), predicate);
            else return null;
        }
        /// <summary>
        /// 根据Lambda表达式predicate条件、每页条数pageSize、页码pageIndex，获取Menu集合
        /// </summary>
        /// <param name="predicate">获取列表的条件</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>Menu集合</returns>
        public List<Menu> GetList(Expression<Func<Menu, bool>> predicate,
           int pageSize, int pageIndex)
        {

            if (predicate != null)
                return LinqUtility.GetInstance().GetList<Menu>
                    (new OneShoppingDataContext(), predicate, pageSize, pageIndex);

            else return null;
        }
        /// <summary>
        /// 添加Menu实体
        /// </summary>
        /// <param name="entity">Menu实体</param>
        /// <returns>是否成功添加</returns>
        public bool Insert(Menu entity)
        {
            return
                LinqUtility.GetInstance().InsertEntity<Menu>(new OneShoppingDataContext(), entity);

        }
        /// <summary>
        /// 更新Menu实体
        /// </summary>
        /// <param name="entity">Menu实体</param>
        /// <returns>是否成功更新</returns>
        public bool Update(Menu entity)
        {
            return
                LinqUtility.GetInstance().UpdateEntity<Menu>(new OneShoppingDataContext(), entity);

        }
        /// <summary>
        ///删除某主键id的Menu实体
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(int id)
        {
            return LinqUtility.GetInstance().DeleteEntity<Menu>(new OneShoppingDataContext(),
                  k => k.MenuId == id);
        }

        //根据用户登录的权限  生成系统左侧树型一二级菜单
        public string GenerateTreeCode(string roleid)
        {
             
            var firstMenuList = MenuBLL.GetInstance().GetList(r=>r.ParentMenu==0).OrderBy(r=>r.MenuDispSeq);
            string code = "<ul>";
            foreach (var r in firstMenuList)
            {
                code = code + "<li><div class=\"li-item\"><em class=\"scm li-ico ic1\"></em>" + r.MenuName + "<span class=\"scm arrow\"></span></div>";
                var secondMenuList = MenuBLL.GetInstance().GetList(k=>k.ParentMenu==r.MenuId).OrderBy(k=>k.MenuDispSeq);
                code = code + "<dl>";
                foreach (var t in secondMenuList)
                {
                    code = code + "<dd><a href='" + t.MenuUrl + "' class=\"dd-item\" target='iframe0'>" + t.MenuName + "<span class=\"scm dd-ar\"></span></a></dd>";
                }
                code = code + "</dl></li>";
            }
            return code;

        }

    }
}